﻿using System.Collections.ObjectModel;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvOpenDistricts.DataService;
using System.ComponentModel;
using System.Linq;

namespace MyFloridaHouse.libSvOpenDistricts.DataDictionary
{

    public class LayersDataDictionary : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        //*********************  Static **************************


        private static LayersDataDictionary _layersDataDictionaryInstance;
        public static LayersDataDictionary layersDataDictionaryInstance
        {
            get { return _layersDataDictionaryInstance; }
        }

        //*********************  Static Interface methods that reference the Instance  **************************


        public static DataDictionaryDTO GetDictionaryIDInfo(int layerID, int dictionaryID)
        {
            //TODO : Need to use LayerID with this
            var dictionaryInfo = (from dict in layersDataDictionaryInstance.layersDataDictionaryList
                                  where dict.dictionaryID == dictionaryID
                                  select dict).First();

            return dictionaryInfo;
        }

        //*********************  Instance **************************


        private bool _isLayersDataDictionaryInitializeCompleted = false;
        public bool isLayersDataDictionaryInitializeCompleted
        {
            get { return _isLayersDataDictionaryInitializeCompleted; }
            set
            {
                if (_isLayersDataDictionaryInitializeCompleted != value)
                {
                    _isLayersDataDictionaryInitializeCompleted = value;
                    if (value)
                    {
                        NotifyPropertyChanged("isRegionsDataDictionaryInitializeCompleted");
                    }
                }
            }
        }

        private ObservableCollection<DataDictionaryDTO> layersDataDictionaryList;

        public LayersDataDictionary(ServiceDAL serviceDAL)
        {
            // when the object is created, store a reference to it in the static class
            // That way anything that needs to access the instance methods can do so without passing around references all over the place
            _layersDataDictionaryInstance = this;


            ServiceDAL.GetLayerDataDictionariesCompleted += OnServiceDALGetLayerDataDictionariesCompleted;
            serviceDAL.GetLayerDataDictionaries();

        }

        public void OnServiceDALGetLayerDataDictionariesCompleted(DataDictionarySTO sto)
        {
            layersDataDictionaryList = sto.dictionary;
            isLayersDataDictionaryInitializeCompleted = true;
        }

    }
}
